<?php
/**
 * SHOP 控制台首页
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');

//分类列表
if($_REQUEST['act'] == 'list'){
	$sql="select * from ".ly()."goods_type where 1=1 order by sort_order desc";
	/*分页开始*/
	$num='10';
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;
	$data=$db->getAll($sql);
	$smarty->assign('info',$data);
	$smarty ->display("goods_type.html");
}elseif($_REQUEST['act'] == 'edit'){
	$c_id=isset($_REQUEST['c_id'])?$_REQUEST['c_id']:0;
	if($_POST){
		$cat_id=isset($_POST['cat_id'])?$_POST['cat_id']:0;//类型id
		$cat_name=isset($_POST['cat_name'])?$_POST['cat_name']:0;//类型名称
		$sort_order=isset($_POST['sort_order'])?$_POST['sort_order']:0;//排序
		$attr_group=isset($_POST['attr_group'])?$_POST['attr_group']:0;//属性分组
		if($cat_id>0){
			$sql="update ".ly()."goods_type set cat_name='{$cat_name}',sort_order='{$sort_order}',attr_group='{$attr_group}' where cat_id='{$cat_id}'";
		}else{
			$sql="insert into ".ly()."goods_type(cat_name,sort_order,attr_group) values('{$cat_name}','{$sort_order}','{$attr_group}')";
		}
		$rs=$db -> query($sql);
		if($rs){
			$href[] = array('text' => "返回列表", 'href' => 'goods_type.php?act=list');
			if($cat_id){$href[] = array('text' => "重新编辑", 'href' => 'goods_type.php?act=edit&c_id='.$cat_id);}
			sys_msg("分类操作成功！",$href);
		}else{
			sys_msg("操作分类失败！");
		}
	}else{
		$sql="select * from ".ly()."goods_type where cat_id='{$c_id}'";
		$data=$db -> getRow($sql);
		$smarty -> assign("show",$data);
		$smarty -> display("goods_type_edit.html");
	}
	
}elseif($_REQUEST['act'] == 'ajax_del'){
	$cat_id=isset($_POST['cat_id'])?$_POST['cat_id']:0;//类型id
	$sql="delete from ".ly()."attribute where cat_id='{$cat_id}'";
	$db->query($sql);
	$sql="delete from ".ly()."goods_type where cat_id='{$cat_id}'";
	$db->query($sql);
	if(@mysql_affected_rows()){
		echo "1";
	}else{
		echo "2";
	}
}elseif($_REQUEST['act'] == 'ajax_enabled'){
	//显示状态
    $cat_id=isset($_POST['cat_id'])?$_POST['cat_id']:0;
    $enabled=isset($_POST['zt'])?$_POST['zt']:0;
    if($enabled==1){$zt='0';}else{$zt='1';}
    $sql="update ".ly()."goods_type set enabled='{$zt}' where cat_id='{$cat_id}'";
    $res=$db->query($sql);
    if($res){
        echo "1";   
    }else{
        echo "2";
    }
}
?>
